import Vue from 'vue'

import loading from '@/components/loading/loading'

const CreateLoading = Vue.extend(loading)

// 实例化loading组件
const instence = new CreateLoading({
    el: document.createElement('div')
})

instence.show = false // show用来控制组件显示和隐藏
const $loading = {
    // 用来让loading显示
    show(msg) {
        instence.show = true
        instence.msg = msg
        document.body.appendChild(instence.$el)
    },
    // 用来让loading隐藏
    hide() {
        instence.show = false
    }
}

export default {
    install() {
        if(!Vue.$loading) {
            Vue.$loading = $loading
        }

        // 如果vue中有$loading方法将方法混入到要使用的组件内
        Vue.mixin({
            created() {
                this.$loading = Vue.$loading
            }
        })
    }
}
